package net.esj.basic.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.esj.basic.exception.MelonRuntimeException;
import net.esj.basic.utils.i18n.LocaleUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ConstantsManager {

	private Map<String, JSONArray> cache = new HashMap<String, JSONArray>();

	private static ConstantsManager instance;
	
	public static void reload() throws JSONException{
		if (instance == null) {
			instance = new ConstantsManager();
			instance.load();
		}
	}

	public static String getValue(String id, String key) {
		try{
			if (instance == null) {
				instance = new ConstantsManager();
				instance.load();
			}
			return instance.getConstant(id, key);
		}catch(JSONException e){
			throw new MelonRuntimeException(e);
		}
	}
	
	public static String getValueInLocal(String id, String key,String local) {
		try{
			if (instance == null) {
				instance = new ConstantsManager();
				instance.load();
			}
			return instance.getConstantInLocal(id, key,local);
		}catch(JSONException e){
			throw new MelonRuntimeException(e);
		}
	}

	public static String getValue(String id, String key,String value) {
		try {
			if (instance == null) {
				instance = new ConstantsManager();
				instance.load();
			}
			return instance.getConstant(id, key,value);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
}
	
	public static JSONArray getJSON(String id)
			throws JSONException {
		if (instance == null) {
			instance = new ConstantsManager();
			instance.load();
		}
		return instance.getJson(id);
	}
	
	public static List<Entity> getList(String id) {
		try{
			if (instance == null) {
				instance = new ConstantsManager();
				instance.load();
			}
			return instance.getEntities(id);
		}catch(JSONException e){
			throw new MelonRuntimeException(e);
		}
	}
	
	public static List<Entity> getList(String id,String local)  {
		try{
			if (instance == null) {
				instance = new ConstantsManager();
				instance.load();
			}
			return instance.getEntities(id,local);
		}catch(JSONException e){
			e.printStackTrace();
		}
		return Collections.EMPTY_LIST;
	}
	

	public void load() throws JSONException{
		File dir = new File(getClass().getResource("/").getPath()+"data/");
		File[] files = dir.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				if(name.endsWith(".json")){
					return true;
				}
				return false;
			}
		});
		for(File file : files){
			JSONArray jo = new JSONArray(ReadFile(file));
			cache.put(file.getName().substring(0, file.getName().length()-5), jo);
		}
	}

	public String getConstant(String id, String key) throws JSONException {
		if(Validators.isEmpty(key)){
			return null;
		}
		JSONArray ja = getJson(id);
		if(ja==null){
			return null;
		}
		for(int i=0;i<ja.length();i++){
			JSONObject o = ja.getJSONObject(i);
			if(o==null){
				continue;
			}
			if(o.get("id").toString().equals(key)){
				return o.getString("text");
			}
		}
		return null;
	}
	
	public String getConstantInLocal(String id, String key,String local) throws JSONException {
		if(Validators.isEmpty(key)){
			return null;
		}
		JSONArray ja = getJson(id+"_"+local);
		if(ja==null){
			ja = getJson(id);
		}
		if(ja==null){
			return null;
		}
		for(int i=0;i<ja.length();i++){
			JSONObject o = ja.getJSONObject(i);
			if(o==null){
				continue;
			}
			if(o.get("id").toString().equals(key)){
				return o.getString("text");
			}
		}
		return null;
	}
	
	public String getConstant(String id, String key,String value) throws JSONException {
		if(Validators.isEmpty(key)){
			return null;
		}
		JSONArray ja = getJson(id);
		if(ja==null){
			return null;
		}
		for(int i=0;i<ja.length();i++){
			JSONObject o = ja.getJSONObject(i);
			if(o==null){
				continue;
			}
			if(o.get("id").toString().equals(key)){
				return o.getString(value);
			}
		}
		return null;
	}

	public JSONArray getJson(String id) throws JSONException {
		JSONArray array = cache.get(id);
		if(array==null){
			array = cache.get(id+"_"+LocaleUtils.locale_zh_CN.toString());
		}
		return array;
	}
	
	public List<Entity> getEntities(String id) throws JSONException{
		List<Entity> list =new ArrayList<Entity>();
		JSONArray array = cache.get(id);
		if(array==null){
			array = cache.get(id+"_"+LocaleUtils.locale_zh_CN.toString());
		}
		if (array != null) {
			for(int i=0;i<array.length();i++){
				JSONObject o = array.getJSONObject(i);
				list.add(new Entity(Integer.valueOf(o.get("id").toString()), o.get("text").toString()));
			}
		}
		return list;
	}
	
	public List<Entity> getEntities(String id,String local) throws JSONException{
		List<Entity> list =new ArrayList<Entity>();
		JSONArray array = cache.get(id+"_"+local);
		if (array != null) {
			for(int i=0;i<array.length();i++){
				JSONObject o = array.getJSONObject(i);
				list.add(new Entity(Integer.valueOf(o.get("id").toString()), o.get("text").toString()));
			}
		}
		return list;
	}
	
	public static JSONObject getJsonObject(String fileName,String value)  {
		try{
			if (instance == null) {
				instance = new ConstantsManager();
				instance.load();
			}
			JSONArray array = instance.getJson(fileName);
			for(int i=0;i<array.length();i++){
				JSONObject o = array.getJSONObject(i);
				if(o==null){
					continue;
				}
				if(o.get("id").toString().equals(value)){
					return o;
				}
			}
		}catch(JSONException e){
			e.printStackTrace();
		}
		return null;
	}

	private String ReadFile(File file) {
		InputStreamReader is = null;
		String laststr = "";
		try {
			// System.out.println("以行为单位读取文件内容，一次读一整行：");
			is = new InputStreamReader(new FileInputStream(file),"UTF-8");
			BufferedReader reader = new BufferedReader(is);
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
//				System.out.println("line " + line + ": " +tempString);
				laststr = laststr +tempString;
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e1) {
				}
			}
		}
		return laststr;
	}
	
	
	public class Entity{
		private int key;
		private String name;
		
		public Entity(int key, String name) {
			super();
			this.key = key;
			this.name = name;
		}
		public int getKey() {
			return key;
		}
		public void setKey(int key) {
			this.key = key;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
	}
	
	public static void main(String[] args) throws JSONException{
		File file = new File("E:\\program\\javaws\\mshrm\\src\\data\\company-quality.json");
		BufferedReader reader = null;
		String laststr = "";
		try {
			// System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				System.out.println("line " + line + ": " +tempString);
//				laststr = laststr +newstr;
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}
}
